package com.apache.funds.service.plugins;

import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.funds.entity.SvmUser;
import com.apache.funds.manager.SvmUserManager;
import com.apache.funds.util.Constants;

/**  
 * 资金账户或口令登录校验
 * @author xhe 创建时间：2015-12-05         
 */
public class FundsAccountLoginPlugin implements PluginConnector {
	private Logger log = LoggerFactory.getLogger(getClass());

	private SvmUserManager svmUserManager;

	@Override
	public Object execute(ParamsVo arg0) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		entity.setMessage("操作失败");
		entity.setEntity("false");

		log.info("资金账户或口令登录校验....");

		Map<String, String> prarmMap = arg0.getParams();
		//校验参数

		String userEname = prarmMap.get("userEname");
		String password = prarmMap.get("password");
		if (Validator.isNull(userEname) || Validator.isNull(password)) {
			entity.setMessage(Constants.Msg.F006+"账户或者口令不能为空");
			entity.setEntity("false");
			return entity;
		}

		ParamsVo<SvmUser> vo = new ParamsVo<SvmUser>();
		vo.setParams("userName", userEname);
		vo.setMethodKey("ByUserName");
		SvmUser user = (SvmUser) svmUserManager.execute(vo);
		if (null == user) {
			entity.setMessage(Constants.Msg.F004);
			entity.setEntity("false");
			return entity;
		}
		String userPass = user.getPassWord();
		if (!password.equals(userPass)) {
			entity.setMessage(Constants.Msg.F011);
			entity.setEntity("false");
			return entity;
		}
		entity.setEntity(JSONObject.fromObject(user));
		entity.setMessage(Constants.Msg.F000 + "账号和口令校验成功！");
		return entity;
	}

	public void setSvmUserManager(SvmUserManager svmUserManager) {
		this.svmUserManager = svmUserManager;
	}
}
